<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>7.4ES6基础-数组操作</title>
</head>
<body>
  <script>
    // forEach
    let arr = [3,4,5]
    // 仅遍历值
    arr.forEach(item=>{
      console.log(item+",")
    })
    // 遍历值和索引
    arr.forEach((item,index)=>{
      console.log(item+"-"+index)
    })
    // find-查询数组中的元素-遍历，直到找到第一个为true的值为值
    console.log(arr.find(item=>item>3))
    // findIndex-返回的是索引值
    console.log(arr.findIndex(item=>item>3))
    // 找不到则返回-1
    console.log(arr.findIndex(item=>item>9))
    // includes-数组中是否包含有某个值
    console.log(arr.includes(3))
    console.log(arr.includes(9))
    // filter-筛选过滤，条件为true的值
    let arr1 = arr.filter(item=>{return item>=4})
    console.log(arr1)
    // every-每个比较都为true,则为true
    console.log(arr.every(item=>item>2))
    console.log(arr.every(item=>item>3))
    // some-只要其中一个为true,则为true
    console.log(arr.some(item=>item>2))
    console.log(arr.some(item=>item>3))
    console.log(arr.some(item=>item>5))
    // reduce-用于求合
    let sum = arr.reduce((total,item)=>{return total+item},0)
    console.log(sum)
    let arr2 = [
      {
        id: 1,
        name: '张三',
        age: 30
      },
      {
        id: 2,
        name: '李四',
        age: 20
      }
    ]
    // map-遍历一次，重新构造新的数组
    let arr3 = arr2.map(item=>{return item.age})
    console.log(arr3)
    // join-拼接成字符串
    console.log(arr.join('-'))
    // flat
    let cc = [3,4,5,[3,4,[7,8]]]
    console.log("flat:"+cc.flat())
    // flatMap
    // 合并数组
    let a1 = [2,3]
    let a2 = [5,6]
    let a3 = [...a1,...a2]
    console.log(a3)
    
  </script>
</body>
</html>